package com.norman.sorry.core.cards;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

import com.norman.sorry.core.moves.IMove;
import com.norman.sorry.core.moves.MoveFactory;
import com.norman.sorry.core.pieces.IPiece;
import com.norman.sorry.core.players.IPlayer;
import com.norman.sorry.core.tiles.ILinkedTile;

/**
 * The class representation of the sorry card. This card allows the player to
 * move any game piece that is in start and place it on any opposing game piece
 * that is not in start or a safe zone.
 * 
 * @author Brian Norman
 * @version 0.0.1 beta
 * @since 0.0.1 beta
 */
public class Sorry extends AbstractCard {

   /**
    * {@inheritDoc}
    * 
    * Returns the moves of each game piece that is in start replacing each
    * possible opposing game piece.
    * 
    * @see com.norman.sorry.core.cards.ICard#getMoves(IPlayer, Map)
    */
   @Override
   public Collection<IMove> getMoves(IPlayer player, Map<IPiece, ILinkedTile> pieces) {
      Collection<IMove> moves = new LinkedList<IMove>();

      for (IPiece pPiece : player.getPieces()) {
         ILinkedTile pTile = pieces.get(pPiece);
         IMove m = null;

         if (pTile == null || !pTile.isStart()) {
            continue;
         }

         for (IPiece ePiece : pieces.keySet()) {
            if (ePiece.equals(pPiece) || ePiece.getColor().equals(pPiece.getColor())) {
               continue;
            }

            ILinkedTile eTile = pieces.get(pPiece);
            if (eTile == null || eTile.isSafe()) {
               continue;
            }

            m = MoveFactory.createSingleMove(pPiece, eTile);
            if (m != null) {
               moves.add(m);
            }
         }
      }

      return moves;
   }

}
